package com.juicygames.shmup.fps;

import com.juicygames.framework.action.UpdateAction;
import com.juicygames.framework.draw.DrawDepth;
import com.juicygames.framework.draw.TextDisplay;
import com.juicygames.framework.draw.drawservice.DrawService;
import com.juicygames.framework.gamepart.GameEntity;
import com.juicygames.framework.gamepart.GameHandler;

public class FpsDisplayer extends GameEntity {
	
	TextDisplay textDisplay;
	
	public void load() {
		textDisplay = addPart(new TextDisplay());
		textDisplay.setX(5);
		textDisplay.setY(10);
		
		groundHandler(addPart(new MyUpdateHandler()));
		
		addPart(new DrawDepth(1));
		getWorld().getPart(DrawService.class).register(textDisplay);
	}

	public TextDisplay getTextDisplay() {
		return textDisplay;
	}
	
	
	class MyUpdateHandler extends GameHandler<UpdateAction> {
		
		public Class<UpdateAction> getActionClassHandled() {
			return UpdateAction.class;
		}
		
		public void handle(UpdateAction update) {
			((FpsDisplayer)parent).getTextDisplay()
					.setText(Float.toString(update.getFramesPerSecond())+" fps");
		}
		
	}

}
